package DS0425;

import java.util.StringTokenizer;

public class Main {
    public static void main(String args[]){
        String date = "I Love World";
        String result = reverse(date);
        String result1 = reverse1(date);
        System.out.println(result);
        System.out.println(result1);

        String text = "i love World";
        String change = captureName(text);
        System.out.println(change);

    }
    //将字母首字母变化为大写
    private static String captureName(String text) {
        //先拆
        StringTokenizer tokenizer = new StringTokenizer(text," ");
        StringBuffer buffer = new StringBuffer();
        while(tokenizer.hasMoreTokens()){
            String word = tokenizer.nextToken();
            buffer.append(FirstChange(word) +" ");
        }
        return buffer.toString();
    }

    private static String FirstChange(String word) {
        char[] arr = word.toCharArray();
        if(arr[0]>='a' && arr[0]<='z'){
            arr[0] = (char)(arr[0]-32);
        }
        return  new String(arr);
    }


    private static String reverse1(String date){
        //先拆分，后逆序，后插入
        StringTokenizer tokenizer = new StringTokenizer(date," ");
        StringBuffer buffer = new StringBuffer();
        while(tokenizer.hasMoreTokens()){
            String word = tokenizer.nextToken();
            buffer.append(reverseword(word)+" ");
        }
        return buffer.toString();
    }

    private static String reverseword(String word) {
        char[] arr = word.toCharArray();
        for(int i=0;i<arr.length/2;i++){
            char team = arr[i];
            arr[i]=arr[arr.length-1-i];
            arr[arr.length-1-i] = team;
        }
        return new String(arr);
    }


    private static String reverse(String date) {
        //先把数全部逆序，然后拆分，再插入
        StringBuffer str = new StringBuffer(date);
        str.reverse();

        StringTokenizer Tokenizer = new StringTokenizer(date," ");
        String[] st = new String[3];
        while(Tokenizer.hasMoreTokens()) {
            int n = 0;
            st[0] = Tokenizer.nextToken();
            n++;
        }
        for(int i=0;i<st.length;i++){
            String team = st[i];
            st[i] = st[st.length-1-i];
            st[st.length-1-i] = team;
        }

           return str.toString();
    }


}
